﻿using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Game.Command;
using Game.Map;
using Game.Map.Extensions;
using Game.Objects;

namespace Game.Surface
{
    static public class SurfaceFieldRenderer
    {
        static Point CENTER = new Point(83, 50);
        
        public static Brush CreateBrush(IMapFieldView field)
        {
            RenderTargetBitmap target = new RenderTargetBitmap(
               300, 300, 96, 96, PixelFormats.Pbgra32);
            
            RenderTerrain(field, target);
            RenderMapObject(field, target);
            RenderHealthBar(field, target);
            //RenderCommandBackground(field, context);
            
            Vehicle v = VehicleExtensionHelper.VehicleX(field).Vehicle;
            if (v != null)
            {
                RenderVehicle(v, target);
            }

            return new ImageBrush { ImageSource = target };
        }

        public static Brush CreateCommandBrush(IMapFieldView field)
        {
            RenderTargetBitmap target = new RenderTargetBitmap(
                300, 300, 96, 96, PixelFormats.Pbgra32);
            
            if (field.IsVisible)
            {
                RenderCommandImage(field, target);
            }
 
            return new ImageBrush(target);
        }

        public static Brush CreateVehicleBrush(IMapFieldView field)
        {
            RenderTargetBitmap target = new RenderTargetBitmap(
               300, 300, 96, 96, PixelFormats.Pbgra32);
            
            {
                RenderVehicle(field.VehicleX().Vehicle, target);
                //RenderCommandImage(field, context, drawingVisual);
            }

            return new ImageBrush { ImageSource = target };
        }

        public static Brush CreateDebugBrush(IMapFieldView field)
        {
            RenderTargetBitmap target = new RenderTargetBitmap(
               300, 300, 96, 96, PixelFormats.Pbgra32);
            
            RenderDebug(target);
            return new ImageBrush { ImageSource = target };
        }
        
        public static Brush CreateCloudBrush(IMapFieldView field)
        {
            return new SolidColorBrush(Colors.LightGray);
        }

        public static Brush CreateDisplayOnlyBrush(IMapField field)
        {
            RenderTargetBitmap target = new RenderTargetBitmap(
               300, 300, 96, 96, PixelFormats.Pbgra32);
            
            {
                RenderTerrain(field, target);
                RenderMapObject(field, target);
                RenderVehicle(field.VehicleX().Vehicle, target);
            }
            
            return new ImageBrush { ImageSource = target };
        }

        public static void RenderCloud(DrawingContext context)
        {
            context.DrawRectangle(
                Brushes.LightGray, 
                new Pen(), 
                new Rect(0, 0, 100, 100));
        }

        public static void RenderHealthBar(
            IMapFieldView field, 
            RenderTargetBitmap target)
        {
            if (field.VehicleX().Vehicle == null) return;

            DrawingVisual v = new DrawingVisual();
            DrawingContext context = v.RenderOpen();
            
            var vehicle = field.VehicleX().Vehicle;
            Debug.Assert(vehicle != null, "vehicle != null");

            double width = ((100f - vehicle.Damage) * 45f) / 100f;
            var r = new Rect { X = 60, Width = width, Height = 10 };
            Brush fill; 
            if (vehicle.Damage > 50)
            {
                fill = new SolidColorBrush(Colors.Red);
            }
            else
            {
                fill = new SolidColorBrush(Colors.Green);
            }
            Pen pen = new Pen(Brushes.Black, 0.3);
            
            context.DrawRectangle(fill, pen, r);
            context.Close();
            
            target.Render(v);
        }

        public static void RenderCommandImage(
            IMapFieldView field, 
            RenderTargetBitmap target)
        {
            if (field.CommandX().Command == null) return;

            DrawingVisual v = new DrawingVisual();
            DrawingContext context = v.RenderOpen();
            
            if (field.CommandX().Command.Name == "Move")
            {
                Color green = Color.FromArgb(0x80, 0x00, 0xFF, 0x00);
                Brush b = new RadialGradientBrush(Colors.WhiteSmoke, green);
                context.DrawEllipse(b, new Pen(), CENTER, 35, 35);
            }
            else
            {
                Color red = Color.FromArgb(0xFF, 0xFF, 0x00, 0x00);
                Brush b = new RadialGradientBrush(red, Colors.Transparent);
                context.DrawEllipse(Brushes.Red, new Pen(), CENTER, 15, 15);
            }
            
            context.Close();
            
            target.Render(v);
        }

        public static void RenderCommandBackground(
            IMapFieldView field, 
            DrawingContext context)
        {
            if (field.CommandX().Command == null) return;

            var r = new Rectangle
            {
                Width = 64, 
                Height = 64, 
                Fill = new SolidColorBrush(Color.FromArgb(150, 128, 128, 128))
            };
            
            DrawingGroup group = VisualTreeHelper.GetDrawing(r);
            Drawing drawing = group.Children[0];
            context.DrawDrawing(drawing);
            
            RenderTargetBitmap bmp = new RenderTargetBitmap(
                64, 64, 96, 96, PixelFormats.Pbgra32);
            bmp.Render(r);
            context.DrawImage(bmp, new Rect(0, 0, 64, 64));
        }

        public static void RenderVehicle(
            Vehicle vehicle, 
            RenderTargetBitmap target)
        {
            if (vehicle == null) return;

            var write = PlayerColorUpdater.GenerateImage(
                vehicle.Picture as BitmapImage, vehicle.Owner, 
                (vehicle.Movement.Value > 0));
            
            DrawingVisual v = new DrawingVisual();
            DrawingContext context = v.RenderOpen();
            
            Brush brush = new ImageBrush(write);
            context.DrawEllipse(
                brush, new Pen(), 
                new Point(83, 50), 50, 50);
            
            context.Close();
            
            target.Render(v);
        }

        public static void RenderDebug(RenderTargetBitmap target)
        {
            DrawingVisual v = new DrawingVisual();
            DrawingContext context = v.RenderOpen();
            
            context.DrawEllipse(
                Brushes.Yellow, new Pen(Brushes.Black, 1),
                new Point(83, 50), 150, 150);
            
            context.Close();
            target.Render(v);
        }
        
        public static void RenderMapObject(
            GameObject gameObject, 
            RenderTargetBitmap target)
        {
            if (gameObject == null || gameObject.Building == null) return;

            var write = PlayerColorUpdater.GenerateImage(
                gameObject.Picture as BitmapImage, gameObject.Building.Owner,
                true);
            
            DrawingVisual v = new DrawingVisual();
            DrawingContext context = v.RenderOpen();
            
            Brush brush = new ImageBrush(write);
            context.DrawEllipse(
                brush, new Pen(),
                new Point(95, 95),
                100, 100);
            
            context.Close();
            
            target.Render(v);
        }

        public static void RenderMapObject(
            IMapField field, 
            RenderTargetBitmap target)
        {
            if (field.ObjectX().GameObject == null) return;

            DrawingVisual v = new DrawingVisual();
            DrawingContext context = v.RenderOpen();
            
            var gameObject = field.ObjectX().GameObject;
            if (gameObject != null && gameObject.Building != null)
            {
                RenderMapObject(gameObject, target);
            }
            else
            {
                ImageSource image = field.ObjectX().GameObject.Picture;
                //context.DrawImage(image, new Rect(0, 0, 48, 48));
                
                Brush brush = new ImageBrush(image);
                context.DrawEllipse(
                    brush, new Pen(),
                    new Point(83, 50),
                    75, 50);
            }
            
            context.Close();
            
            target.Render(v);
        }

        public static void RenderTerrain(
            IMapField field, 
            RenderTargetBitmap target)
        {
            DrawingVisual v = new DrawingVisual();
            DrawingContext context = v.RenderOpen();
            
            Brush brush = new ImageBrush(field.TerrianX().TerrainType.ImageSource);
            context.DrawRectangle(brush, new Pen(), new Rect(0, 0, 300, 300));
            context.Close();
            
            target.Render(v);
        }
        
        public static void RenderBorder(
            RenderTargetBitmap target)
        {
            DrawingVisual v = new DrawingVisual();
            DrawingContext context = v.RenderOpen();
            
            context.DrawRectangle(
                Brushes.Transparent,
                new Pen(Brushes.Black, 1),
                new Rect(new Point(0, 0), new Size(300, 300) ));
            
            context.Close();
            target.Render(v);
        }
    }
}
